@file:Suppress("MagicNumber")

package com.gitee.wsl.compose.chart.sample

import androidx.compose.foundation.layout.absolutePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.unit.dp
import com.gitee.wsl.common.ui.draw.style.LineStyle
import com.gitee.wsl.compose.chart.ChartLayout
import com.gitee.wsl.compose.chart.ExperimentalKoalaPlotApi

import com.gitee.wsl.compose.chart.xy.XYGraph
import com.gitee.wsl.compose.chart.xy.axis.model.FloatLinearAxisModel
import com.gitee.wsl.compose.chart.xy.axis.model.LogAxisModel
import com.gitee.wsl.compose.chart.xy.line.LinePlot
import com.gitee.wsl.compose.chart.xy.model.PointEntry
import com.gitee.wsl.compose.modifier.layout.VerticalRotation
import com.gitee.wsl.compose.modifier.layout.rotateVertically
import com.gitee.wsl.compose.ui.widget.Symbol
import com.gitee.wsl.compose.modifier.layout
import com.gitee.wsl.ext.base.ifValue


@OptIn(ExperimentalKoalaPlotApi::class)
val xyLogLineSampleView = object : SampleView {
    override val name: String = "XY Line - Log Axis"

    override val thumbnail = @Composable {
        ThumbnailTheme {
            XYSamplePlot(true, name)
        }
    }

    override val content: @Composable () -> Unit = @Composable {
        XYSamplePlot(false, "Fibonacci Sequence")
    }
}

private val YAxisRange = -1..4
private val XAxisRange = -0.5f..15.5f

@OptIn(ExperimentalKoalaPlotApi::class)
@Composable
private fun XYSamplePlot(thumbnail: Boolean, title: String) {
    ChartLayout(
        modifier = paddingMod,
        title = { ChartTitle(title) }
    ) {
        XYGraph(
            xAxisModel = FloatLinearAxisModel(
                XAxisRange,
                minimumMajorTickIncrement = 1f,
                minorTickCount = 0
            ),
            yAxisModel = LogAxisModel(YAxisRange),
            xAxisLabels = {
                if (!thumbnail) {
                    AxisLabel("${it.toInt()}", Modifier.padding(top = 2.dp))
                }
            },
            xAxisTitle = { if (!thumbnail) AxisTitle("Position in Sequence") },
            yAxisLabels = {
                if (!thumbnail) AxisLabel(it.toString(), Modifier.absolutePadding(right = 2.dp))
            },
            yAxisTitle = {
                if (!thumbnail) {
                    AxisTitle(
                        "Value",
                        modifier = Modifier
                            .layout {
                                rotateVertically(VerticalRotation.COUNTER_CLOCKWISE)
                            }
                            .padding(bottom = padding)
                    )
                }
            },

        ) {
            LinePlot(
                data = fibonacciExtended.mapIndexed { index, fl ->
                    PointEntry(index.toFloat(), fl)
                },
                lineStyle = LineStyle(brush = SolidColor(Color.Black), strokeWidth = 2.dp),
                symbol = { point,selected ->
                    Symbol(
                        shape = CircleShape,
                        fillBrush = SolidColor(Color.Black),
                        modifier = Modifier.size(8.dp * selected.ifValue(1.5f,1f)).then(
                            if (!thumbnail) {
                                Modifier.hoverableElement {
                                    HoverSurface { Text(point.y.toString()) }
                                }
                            } else {
                                Modifier
                            }
                        )
                    )
                },
            )
        }
    }
}
